package zhklappoint.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.binarywang.java.emoji.EmojiConverter;

import zhklappoint.annotation.IgnoreAuth;
import zhklappoint.entity.Company;
import zhklappoint.entity.DrainageObj;
import zhklappoint.enums.ENUM_ISDELETE;
import zhklappoint.enums.ENUM_SUCESS;
import zhklappoint.service.DrainageService;
import zhklappoint.service.InsertIntoCompanyInfoService;
import zhklappoint.util.ResponseBean;
import zhklappoint.util.Tools;

/***
 * 后台接口
 * @author eagle
 *
 */
@Controller
@CrossOrigin
@RequestMapping(value="/uni/api",produces = "text/html;charset=UTF-8")
public class AdminController {
	
	@Autowired
	private InsertIntoCompanyInfoService ics;
	@Autowired
	private DrainageService drainageService;
	
	
	//首页获取商户列表
		@IgnoreAuth
		@RequestMapping(value="/getHeadInfo",produces = "text/html;charset=UTF-8")
		@ResponseBody
		public String getHeadInfo(String keyWord,Integer page,Integer size) throws Exception {
//			System.out.println("key:"+keyWord+"page:"+page+"size:"+size);
			Company c=new Company();
			if(!ObjectUtils.isEmpty(keyWord)) {
				c.setCompanyName(keyWord);
				if(Tools.isNumeric(keyWord)) {
					c.setBossPhome(keyWord);
				}else {
					c.setBossName(keyWord);
				}
			}
			Page<Company> pcs = ics.getCompanyListByMore(c, page, size);
			List<Company> lcs = pcs.getContent();
			for(Company cany:lcs) {
				if(!ObjectUtils.isEmpty(cany.getImgUrl())) {//用户背景大图只有一张
					cany.setImgUrl(Tools.splitString(cany.getImgUrl(), "backimg","indexpage",0));
				}
				if(!ObjectUtils.isEmpty(cany.getLogoImage())) {//单位logo
					cany.setLogoImage(Tools.splitString(cany.getLogoImage(), "backimg","indexpage",0));
				}
				if(!ObjectUtils.isEmpty(cany.getSwitchImg())) {
					String[] imgList=cany.getSwitchImg().split(",");
					String switchImgspath="";
					for(int i=0;i<imgList.length;i++) {
						if(i==imgList.length-1) {
							switchImgspath=switchImgspath+Tools.splitString(imgList[i], "backimg","indexpage",0);
						}else {
							switchImgspath=Tools.splitString(imgList[i], "backimg","indexpage",0)+",";
						}
					}
					cany.setSwitchImg(switchImgspath);
				}
			}
			return ResponseBean.of(lcs, ENUM_SUCESS.SUCESS.value, "获取数据成功");
		}
		@IgnoreAuth
		@RequestMapping(value="/getDrainageInfo",produces = "application/x-www-form-urlencoded;charset=UTF-8")
		@ResponseBody
		public String getDrainageInfo() {
			DrainageObj drainage = new DrainageObj();
			Page<DrainageObj> pdos = drainageService.getDrainageObj(drainage, 1, 10);
			return ResponseBean.of(pdos.getContent(), ENUM_SUCESS.SUCESS.value, "获取招募页信息成功");
		}
		//填写用户浏览平台的相关介绍页 引流入住 
		@IgnoreAuth
		@RequestMapping(value="/addDrainageData",produces = "application/x-www-form-urlencoded;charset=UTF-8",method=RequestMethod.POST)
		@ResponseBody
		public String addDrainageData(DrainageObj drainageOjb) {
			EmojiConverter emojiConverter = EmojiConverter.getInstance();
			if(ObjectUtils.isEmpty(drainageOjb.getContent())) {
				drainageOjb.setContent("平台正在建设中........尽请期待");
			}else {
				drainageOjb.setContent(emojiConverter.toHtml(drainageOjb.getContent()));
			}
			DrainageObj obj = new DrainageObj();
			Page<DrainageObj> pdojs = drainageService.getDrainageObj(obj, 1, 10);
			if(pdojs.getTotalElements()==1) {
				obj=pdojs.getContent().get(0);
				obj.setContent(drainageOjb.getContent());
				drainageService.saveDrainageObj(obj);
			}else {
				for(DrainageObj o :pdojs) {
					o.setIsdelete(ENUM_ISDELETE.DELETED.value);
					drainageService.saveDrainageObj(o);
				}
				drainageService.saveDrainageObj(drainageOjb);
			}
			return ResponseBean.of(null, ENUM_SUCESS.SUCESS.value, "招募页排版已更新");
		}

}